<template>
  <div class="wrapper">
    <van-nav-bar
      title="修改个人信息"
      right-text="保存"
      @click-right="onClickRight"
      left-text
      left-arrow
      @click-left="$router.go(-1)"
    />
    <div class="box" v-if="num == 0">
      <van-field v-model="value" />
    </div>
    <div class="box" v-if="num == 1">
      <van-radio-group v-model="value">
        <van-cell-group>
          <van-cell title="男" clickable @click="value = '0'">
            <template #right-icon>
              <van-radio name="0" />
            </template>
          </van-cell>
          <van-cell title="女" clickable @click="value = '1'">
            <template #right-icon>
              <van-radio name="1" />
            </template>
          </van-cell>
        </van-cell-group>
      </van-radio-group>
    </div>
    <div class="box" v-if="num == 2">
      <van-checkbox-group v-model="result" @change="changed">
        <van-checkbox
          v-for="(item,index) in attrData.value"
          :key="index"
          :name="item.id"
        >{{item.name}}</van-checkbox>
      </van-checkbox-group>
    </div>
  </div>
</template>

<script>
import { setInfoPort, getAttributePort } from '@/api/main'
export default {
  data() {
    return {
      value: '',
      result: [],
      num: null,
      attrData: [],
      attrValId: [],
      attr_id: 2
    }
  },
  computed: {},
  created() {},
  mounted() {
    let tag = this.$route.query.tag
    if (tag == 'nickname') {
      this.num = 0
      let value = this.$route.query.value

      this.value = value
      console.log(value)
    }
    if (tag == 'sex') {
      this.num = 1
      let value = this.$route.query.value

      this.value = value
      console.log(value)
    }
    if (tag == 'subjects') {
      this.num = 2
      let value = JSON.parse(this.$route.query.value)
      value.forEach(item => {
        this.result.push(item.attr_val_id)
      })
      this.value = value
      
      console.log(this.result)
    }

    this.gettAttribute()
  },
  watch: {},
  methods: {
    //   获取学科/年级数据
    async gettAttribute() {
      let res = await getAttributePort()
      this.attrData = res.data[1]
      console.log(this.attrData)
    },
    // 点击保存触发事件
    async onClickRight() {
      if (this.num == 0) {
        let res = await setInfoPort({ nickname: this.value })
        console.log(res)
        if (res.code == 200) {
          this.$router.push('/info')
        }
      }
      if (this.num == 1) {
        let res = await setInfoPort({ sex: this.value })
        console.log(res)
        if (res.code == 200) {
          this.$router.push('/info')
        }
      }
      if (this.num == 2) {
        let arr = []
        this.result.forEach(item => {
          arr.push({ attr_id: this.attr_id, attr_val_id: item })
        })
        console.log(arr)
        let res = await setInfoPort({ user_attr: JSON.stringify(arr) })
        console.log(res)
        if (res.code == 200) {
          this.$router.push('/info')
        }
      }
    },
    changed(e) {
      console.log(e)
    }
  },
  components: {}
}
</script>

<style lang="scss" scoped>
.wrapper {
  width: 750px;
  background: #f4f4f4;
}
.box {
  margin: 10px 0;
}
.van-field,.van-cell{
  font-size: 20px;
}
.van-checkbox {
  background: #fff;
  width: 720px;
  box-sizing: border-box;
  padding: 0 15px;
  height: 40px;
  font-size: 20px;
}
</style>
